/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.saber.certificacao.ejbs.jms;

import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.naming.NamingException;
import br.com.saber.certificacao.ejbs.jms.jmsEmailUtilitarios.interfaces.EmailSendInterface;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
/**
 *
 * @author gbvbahia
 */
@MessageDriven(mappedName = "jms/EmailSaber", activationConfig = {
    @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
    @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
    @ActivationConfigProperty(propertyName = "messageSelector", propertyValue = "MessageFormat = 'saber_msg'")
})
public class EmailsJms implements MessageListener {

    //private String characterCoding = "iso-8859-1";  
    @Resource(name="emailSenha")
    private String senhaEmail;
        
   @Resource(name = "mail/google")
    private Session mailSession;
      
    public EmailsJms() {
    }

    @Override
    public void onMessage(Message message) {
        EmailSendInterface email = null;
        ObjectMessage objMsg = (ObjectMessage) message;
        try {
            email = (EmailSendInterface) objMsg.getObject();
            sendMail(email.getEmail(), email.getSubject(), email.getBody());
        } catch (JMSException ex) {
            Logger.getLogger(EmailsJms.class.getName()).log(Level.WARNING, "onMessage 1º catch", ex);
        } catch (MessagingException msgEx) {
            Logger.getLogger(EmailsJms.class.getName()).log(Level.WARNING, "onMessage 2º catch", msgEx);
        } catch (NamingException nEx) {
            Logger.getLogger(EmailsJms.class.getName()).log(Level.WARNING, "onMessage 3º catch", nEx);
        }
    }

    private void sendMail(String e_mail, String subject, String body) throws NamingException, MessagingException {
        javax.mail.Message msg = new MimeMessage(mailSession);
         try {
            msg.setSubject(subject);
            msg.setRecipient(javax.mail.Message.RecipientType.TO,
                    new InternetAddress(e_mail));
            
            BodyPart messageBodyPart = new MimeBodyPart();
            messageBodyPart.setContent(body, "text/html");
            Multipart multipart = new MimeMultipart();
            multipart.addBodyPart(messageBodyPart);
            msg.setContent(multipart);
            Transport.send(msg);
        
        Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Email enviado com sucesso! ==> '{'{0}'}'", e_mail);
       }catch(Exception e){
           throw new MessagingException(e_mail, e);
       }
    }
}
